#!/usr/bin/env python
# -*-coding:utf-8-*-
# File    : liyuqi.py
# Date    : 2021-02-10
# Author  : Tom.Lee

import os
from random import randrange


class Doubleball:
    def __init__(self):
        # 初始化函数，从文件读取已存在的双色球数据
        self.shuangsqs = []
        if os.path.isfile("ssq.csv"):
            fs = open("ssq.csv")
            for line in fs.readlines():
                numlist = convertdcb(line)
                if numlist:
                    self.shuangsqs.append(numlist)
            fs.close()

    def save(self):
        fs = open("ssq.csv", "w")
        for line in self.shuangsqs:
            fs.write(','.join([str(n) for n in line]) + "\n")
        fs.close()

    def clear(self):
        self.shuangsqs = []

    def random_new(self):
        randarray = []
        creatcount = 0
        while creatcount < 6:
            x = randrange(1, 33, 1)
            if x not in randarray:
                creatcount += 1
                randarray.append(x)
        randarray.sort()
        randarray.append(randrange(1, 16, 1))
        self.shuangsqs.append(randarray)

    def change(self, index, newnums):
        self.shuangsqs[index] = newnums


def input_balls():
    while True:
        nums = input("请输入一注双色球，以逗号分隔：")
        numblist = convertdcb(nums)
        if numblist:
            return numblist
        print("您输入有误，请重新输入")


def convertdcb(dcbstr):
    try:
        numlist = [int(n) for n in dcbstr.split(',')]
    except Exception:
        pass
    else:
        if len(numlist) == 7:
            newlist = list(set(numlist[:6]))
            if len(newlist) == 6:
                newlist.sort()
                if newlist[0] >= 1 and newlist[-1] <= 33 and (
                        1 <= numlist[-1] <= 16):
                    newlist.append(numlist[-1])
                    return newlist


if __name__ == '__main__':
    dcb = Doubleball()
    is_change = False
    while True:
        choice = input("a.手输一注\tb.机选一注\tc.编辑\td.清空\te.存档\tf.退出")
        if choice.lower() == 'a':
            dcb.shuangsqs.append(input_balls())
            is_change = True
        elif choice.lower() == 'b':
            dcb.random_new()
            is_change = True
            print("添加成功")
        elif choice.lower() == 'c':
            while True:
                index = input("请输入您要修改的双色球序号并回车")
                try:
                    index = int(index)
                except Exception as e:
                    print("您输入有误，请重新输入")
                    print(e)
                    continue
                else:
                    if 0 < index < len(dcb.shuangsqs):
                        dcb.shuangsqs[index - 1] = input_balls()
                        break
            is_change = True

        elif choice.lower() == 'd':
            dcb.shuangsqs = []
            is_change = True
        elif choice.lower() == 'e':
            dcb.save()
            is_change = False
            print("保存成功")
        elif choice.lower() == 'f':
            if is_change == True:
                ch = input("您还有数据未保存，是否保存数据，是请按y键，否请任意键并回车。")
                if ch.lower() == "y":
                    dcb.save()
                    is_change = False
            exit()
        else:
            print(dcb.shuangsqs)
